/*
 * Repertoire.java
 *
 * Created on 18 novembre 2007, 11:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Modele;

import java.io.File;
import java.util.ArrayList;
import java.util.Observable;

public class Repertoire extends Observable implements EnsembleImage{
   
    private ArrayList<ModeleImage> images;
    private String adresse;
    private String nom;
    private File repertoire;
   
    /**
     * 
     *creer une instance de repertoire
     *@param String adr
     *@param String nm
     */
    public Repertoire( String adr) {
        images = new ArrayList();
        this.adresse = adr;
        this.repertoire = new File(adr);
        this.nom = repertoire.getName();
        this.setArrayList();
    }
   
    public void setArrayList(){
        File[] list = repertoire.listFiles();
        
        for(int i = 0; i < list.length; i++){
            if (list[i].isFile()){
                String file = list[i].getName();
                int Index = file.lastIndexOf('.');
                if (Index >= 0) {
                    String ext =  file.substring(Index + 1);
                    if (ext.toUpperCase().equals("JPG") || ext.toUpperCase().equals("GIF") || ext.toUpperCase().equals("JPEG")){
                        ModeleImage img = new ModeleImage(list[i]);
                        images.add(img);
                        System.out.println("Ajout de : " + img.getNom());
                    }
                        
                }
            }
        }
    }
    
    public ArrayList getImages(){
        return images;
    }
    
    public String getAbsoluteAdr(){
        return repertoire.getAbsolutePath();
    }
    
    public void modifierNom(String nom){
        this.nom = nom;
        File dest = new File(this.nom);
        repertoire.renameTo(dest);
        repertoire = dest;
    }
    
    public void ajouterImage(ModeleImage img){
        images.add(img);
    }
    
    public void supprimerImage(ModeleImage img){
        images.remove(img);
    }
/*
    public void trierImage(int critere){
        
    }
    */
    @Override
    public String toString(){
        return this.nom;
    }
    
    public ArrayList getListofImage(){
        return this.images;
    }
    
    public boolean equals(String s2){
        String s1 = this.getAbsoluteAdr();
        if (s2.length() != s1.length())
            return false;
        
        for(int i = 0; i<s2.length(); i++){
            if (s1.charAt(i) != s2.charAt(i))
            return false;
        }
        
        return true;
    }
    
    /*
    public  static void main(String args[]) {
        Repertoire rep = new Repertoire("D:\\Mes Documents\\test");
        ArrayList imgs = this.getImages();
        
        Iterator it = imgs.iterator();
        while(it.hasNext()){
            ModeleImage img = (ModeleImage) it.next();
            System.out.println(img.getNom());
        }
        
        
    }*/
}
